package com.example.chirdrenserver.Service.AdminService.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.chirdrenserver.Pojo.dto.BasePage;
import com.example.chirdrenserver.Pojo.entity.Permission;
import com.example.chirdrenserver.Pojo.entity.Role;
import com.example.chirdrenserver.Pojo.entity.RolePermission;
import com.example.chirdrenserver.Pojo.entity.UserRole;
import com.example.chirdrenserver.Pojo.vo.RolePermissionVO;
import com.example.chirdrenserver.Service.AdminService.PermissionService;
import com.example.chirdrenserver.Service.AdminService.RolePermissionService;
import com.example.chirdrenserver.Service.AdminService.RoleService;
import com.example.chirdrenserver.Service.AdminService.UserRoleService;
import com.example.chirdrenserver.exception.BaseException;
import jakarta.validation.groups.Default;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.chirdrenserver.mapper.RoleMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: Yu
 * @CreateTime: 2025-07-01
 * @Description:
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;



    @Override
    @Transactional
    public void removeRole(String id) {
        if (userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getRoleId, id)).size() > 0) {
            throw new BaseException("存在用户为此角色");

        }
        rolePermissionService.remove(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, id));
        if (!remove(new LambdaQueryWrapper<Role>().eq(Role::getRoleId, id))) {
            throw new BaseException("删除失败");
        }

    }

    @Override public Set<RolePermissionVO> listRolePermission(BasePage basePage) {
        Set<RolePermissionVO> rolePermissionVOs = new HashSet<>();
        List<RolePermission>  rolePermissions= rolePermissionService.page(basePage.page()).getRecords();
        rolePermissions.forEach(rolePermission -> {
            RolePermissionVO rolePermissionVO = new RolePermissionVO();
            Role role = getOne(new LambdaQueryWrapper<Role>().eq(Role::getRoleId, rolePermission.getRoleId()));
            Permission permission = permissionService.getOne(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionId, rolePermission.getPermissionId()));
            if(BeanUtil.isEmpty(role))
            {
                throw new BaseException("角色不存在");
            }
            if(BeanUtil.isEmpty(permission))
            {
                throw new BaseException("角色对应的权限不存在");
            }
            BeanUtil.copyProperties(role, rolePermissionVO);
            BeanUtil.copyProperties(permission, rolePermissionVO);
            rolePermissionVOs.add(rolePermissionVO);
        });
        return rolePermissionVOs;

    }

    @Override
    @Transactional
    public void add(Role role) {
        save(role);
        Permission defaultId = permissionService.getOne(new LambdaQueryWrapper<Permission>().eq(Permission::getPermissionSign, "permission:default"));
        RolePermission rolePermission = new RolePermission();
        rolePermission.setPermissionId(defaultId.getPermissionId());
        rolePermission.setRoleId(role.getRoleId());
        rolePermissionService.save(rolePermission);
    }
}
